package controller;

import api.WxTransfersApi;
import common.pojo.CommonResult;
import common.spring.redis.RedisUnits;
import common.utils.CommomRandom;
import common.utils.JsonUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.WxUserInfo;
import pojo.form.TransfersInfoAddForm;
import service.TransfersInfoService;
import service.WxUserInfoService;
import service.cfg.WxCfg;
import utils.JsonUtil;
import utils.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * 微信转账
 * Created by asus on 2018/12/2.
 */
@RequestMapping("/wx/transfers")
@Controller
public class WxTransfersController {
    private final Logger LOG = LoggerFactory.getLogger(WxTransfersController.class);

    @Autowired
    WxTransfersApi transfersApi;
    @Autowired
    RedisUnits redisUnits;
    @Autowired
    WxUserInfoService infoService;
    @Autowired
    TransfersInfoService transfersInfoService;
    @Autowired
    WxCfg wxCfg;

    @RequestMapping(value = "/")
    @ResponseBody
    public CommonResult transfers(
            @RequestParam(value = "openId") String openId,
            @RequestParam(value = "orderCode") String orderCode,
            @RequestParam(value = "amount") String amount,
            @RequestParam(value = "name") String name,
            @RequestParam(value = "desc") String desc
    ) {

        if (StringUtils.isBlank(openId)) {
            return CommonResult.returnFailsWrapper("请传入openId");
        }
        if (StringUtils.isBlank(name)) {
            return CommonResult.returnFailsWrapper("请传入name");
        }
        if (StringUtils.isBlank(orderCode)) {
            return CommonResult.returnFailsWrapper("请传入orderCode");
        }
        if (StringUtils.isBlank(amount)) {
            return CommonResult.returnFailsWrapper("请传入amount");
        }
        if (StringUtils.isBlank(desc)) {
            return CommonResult.returnFailsWrapper("请传入desc");
        }
        LOG.warn("--转帐 ----start-");

        TransfersInfoAddForm infoAddForm = new TransfersInfoAddForm(name, desc, wxCfg.getMch_id(), wxCfg.getApp_id(), orderCode, new Date(), amount);
        int num = transfersInfoService.addTransfersInfo(infoAddForm);
        if (num < 0) {
            return CommonResult.returnFailsWrapper("转账记录添加失败");
        }
        String paymentTime="";
        String paymentNo="";
        try {
            Map param = transfersApi.transfers(orderCode, getIntNum(amount),name,openId, desc);
            if ("SUCCESS".equals(param.get("result_code").toString()) && "SUCCESS".equals(param.get("return_code"))){
                LOG.warn("--转帐 ----success-");
                paymentTime=param.get("payment_time").toString();
                paymentNo=param.get("payment_no").toString();
                transfersInfoService.updateTransfersInfo(paymentNo,paymentTime,orderCode);
                return CommonResult.returnSuccessWrapper("转账成功");
            }else {
                LOG.warn("---转帐失败----"+JsonUtil.toJson(param));
                return CommonResult.returnFailsWrapper("转帐失败");
            }
        } catch (Exception e) {
            return CommonResult.returnFailsWrapper("转帐失败");
        }
    }


    public String getIntNum(String amount) {
        try {
            Double dou = NumberUtils.toDouble(amount) * 100;
            int num = dou.intValue();
            return "" + num + "";
        } catch (Exception e) {
            LOG.warn(" --- amount转换异常 --");
        }
        return "";
    }
}
